home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wil4c10.zip / WHO_C.C < prev    next >
C/C++ Source or Header  |  1997-07-26  |  8KB  |  308 lines

  1. /*
  2. **  WHO_C.C [WHOIS Client] ---
  3. */
  4.  
  5. #include <windows.h>
  6. #include <winsock.h>
  7.  
  8. #include "wil.h"
  9. #include "message.h"
  10. #include "paint.h"
  11. #include "about.h"
  12. #include "str.h"
  13.  
  14. #ifdef WIN32
  15. #define USE_INS HINSTANCE
  16. #define USE_PTR PSTR
  17. #else
  18. #define USE_INS HANDLE
  19. #define USE_PTR LPSTR
  20. #endif
  21.  
  22. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  23.  
  24. /* public globals */
  25.  
  26. HWND hMainWnd;            /* main window handle */
  27.  
  28. #define BS          8
  29. #define LF         10
  30. #define CR         13
  31. #define MAX_STR    40
  32. #define MAX_BUF   128
  33. #define ONE_SEC  1000
  34. #define TEN_SEC 10000
  35.  
  36. #define WHOIS_PORT    5001
  37.  
  38. static HMENU hMenu;
  39. static USE_INS hInstance;
  40. static int WinWidth = 8 * NCOLS;
  41. static int WinHeight = 12 * NROWS + 48;
  42. static char Temp[MAX_BUF+8];
  43. static int  InBufLen = 0;
  44. static char InBuffer[MAX_BUF+1];
  45. static char User[MAX_STR] = "\0";
  46. static char Host[MAX_STR] = "\0";
  47. static SOCKET Socket = 0;
  48. static ULONG  HostAddr = 0;
  49. static LPSTR  Ptr;
  50.  
  51. /* miscellaneous functions */
  52.  
  53. static void Add2Buffer(char Chr)
  54. {/* add char to input buffer */
  55.  switch(Chr)
  56.    {case BS:
  57.       if(InBufLen>0)
  58.         {/* backup on screen */
  59.          DisplayChar(BS);
  60.          /* remove last char from buffer */
  61.          InBufLen--;
  62.         }
  63.       break;
  64.     default:
  65.       /* display char */
  66.       DisplayChar(Chr);
  67.       /* put into buffer */
  68.       if(InBufLen<MAX_BUF) InBuffer[InBufLen++] = Chr;
  69.       break;
  70.    }
  71. }
  72.  
  73. /* display error message */
  74.  
  75. static void DisplayError(int Code, LPSTR Msg)
  76. {DisplayString("ERROR: ");
  77.  if(Msg) DisplayString(Msg);
  78.  if(Code)
  79.    {wilErrorText(Code,(LPSTR)Temp,50);
  80.     DisplayLine((LPSTR)Temp);
  81.    }
  82. }
  83.  
  84. /* WinMain */
  85.  
  86. #ifdef WIN32
  87. int WINAPI
  88. #else
  89. int PASCAL
  90. #endif
  91. WinMain(USE_INS hInst, USE_INS hPrevInstance,
  92.         USE_PTR szCmdLine,  int nCmdShow)
  93. {WNDCLASS  wc;
  94.  MSG msg;
  95.  BOOL Result;
  96.  if(!hPrevInstance)
  97.    {/* register main window class */
  98.     wc.style = CS_HREDRAW | CS_VREDRAW;
  99.     wc.lpfnWndProc = MainWndProc;
  100.     wc.cbClsExtra = 0;
  101.     wc.cbWndExtra = 0;
  102.     wc.hInstance = hInst;
  103.     wc.hIcon = LoadIcon(hInst, "HostIcon");
  104.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  105.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  106.     wc.lpszMenuName =  "HostMenu";
  107.     wc.lpszClassName = "HostWClass";
  108.     Result = RegisterClass(&wc);
  109.     if(!Result) return FALSE;
  110.    }
  111.  
  112.  /* create main window */
  113.  hInstance = hInst;
  114.  hMainWnd = CreateWindow(
  115.         "HostWClass",   "WHOIS Client",    WS_OVERLAPPEDWINDOW,
  116.         CW_USEDEFAULT,  CW_USEDEFAULT,
  117.         WinWidth,       WinHeight,
  118.         NULL,           NULL,
  119.         hInstance,      NULL);
  120.  ShowWindow(hMainWnd, nCmdShow);
  121.  UpdateWindow(hMainWnd);
  122.  hMenu = GetMenu(hMainWnd);
  123.  
  124.  /* window control loop */
  125.  
  126.  while(GetMessage(&msg,NULL,0,0))
  127.    {
  128.     TranslateMessage(&msg);
  129.     DispatchMessage(&msg);
  130.    }
  131.  return (msg.wParam);
  132. } /* end WinMain */
  133.  
  134. #ifdef WIN32
  135. LRESULT CALLBACK
  136. #else
  137. long FAR PASCAL
  138. #endif
  139. MainWndProc(HWND hWindow,UINT iMsg,WPARAM wParam,LPARAM lParam)
  140. {int Code;
  141.  HDC hDC;
  142.  PAINTSTRUCT ps;
  143. #ifdef WIN32
  144. #else
  145.  static FARPROC lpfnAboutDlgProc;
  146. #endif
  147.  hMainWnd = hWindow;
  148.  switch (iMsg)
  149.     {case WM_CREATE:
  150. #ifdef WIN32
  151. #else
  152.        /* create thunk for Win16 */
  153.        lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc,hInstance);
  154. #endif
  155.       /* initialize paint module */
  156.       PaintInit();
  157.       /* attach WINSOCK */
  158.       DisplayString("Attaching WINSOCK...");
  159.       Code = wilAttach();
  160.       DisplayLine("OK");
  161.       if(Code<0) DisplayError(Code,"wilAttach fails:");
  162.       else
  163.         {wsprintf((LPSTR)Temp," Description: %s", wilGetDescription() );
  164.          DisplayLine((LPSTR)Temp);
  165.          wsprintf((LPSTR)Temp," My HostName: %s", wilGetMyHostName() );
  166.          DisplayLine((LPSTR)Temp);
  167.          wsprintf((LPSTR)Temp," My HostAddr: %s", wilGetMyHostDotted(0) );
  168.          DisplayLine((LPSTR)Temp);
  169.         }
  170.       break;
  171.  
  172.      case WM_COMMAND:
  173.          switch(wParam)
  174.            {case MSG_ABOUT :
  175. #ifdef WIN32
  176.                DialogBox(hInstance, "AboutBox", hMainWnd, AboutDlgProc);
  177. #else
  178.                DialogBox(hInstance, "AboutBox", hMainWnd, lpfnAboutDlgProc);
  179. #endif
  180.                return 0;
  181.  
  182.             case MSG_EXIT:
  183.               wilRelease();
  184.               DestroyWindow(hMainWnd);
  185.               break;
  186.  
  187.             case MSG_DEBUG:
  188.               Code = wilDebug(0);
  189.               wsprintf((LPSTR)Temp,"Debug(0) returned %d",Code);
  190.               DisplayLine((LPSTR)Temp);
  191.               break;
  192.  
  193.             case MSG_WHOIS:
  194.               InBufLen = 0;
  195.               DisplayString("WHOIS: Enter user@host:");
  196.               break;
  197.  
  198.            }
  199.          break;
  200.  
  201.     case WM_PAINT:
  202.       HideCaret(hMainWnd);
  203.       hDC = BeginPaint(hMainWnd, &ps);
  204.       SetMapMode(hDC,MM_ANISOTROPIC);
  205.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  206.       PaintMain(hDC,&ps);
  207.       EndPaint(hMainWnd,&ps);
  208.       SetCaretPos(PaintGetColPos(),PaintGetRowPos());
  209.       ShowCaret(hMainWnd);
  210.       break;
  211.  
  212.     case WM_DESTROY:
  213.       PostQuitMessage(0);
  214.       break;
  215.  
  216.     case WM_CHAR:
  217.       if(wParam==CR)
  218.         {/* do the CR */
  219.          DisplayChar((char)wParam);
  220.          /* user has completed input */
  221.          DisplayChar(LF);
  222.          InBuffer[InBufLen] = '\0';
  223.          wsprintf((LPSTR)Temp,"WHOIS %s", InBuffer);
  224.          DisplayLine((LPSTR)InBuffer);
  225.          /* extract user & host names */
  226.          Ptr = StringChar((LPSTR)InBuffer,'@');
  227.          if(Ptr==NULL)
  228.            {DisplayError(Code, "Cannot recognize User@Domain");
  229.             break;
  230.            }
  231.          *Ptr = '\0';
  232.          lstrcpy((LPSTR)User, (LPSTR)InBuffer);
  233.          lstrcpy((LPSTR)Host, (LPSTR)(++Ptr));
  234.          wsprintf((LPSTR)Temp,"User ='%s'", (LPSTR)User);
  235.          DisplayLine(Temp);
  236.          wsprintf((LPSTR)Temp,"Domain ='%s'", (LPSTR)Host);
  237.          DisplayLine(Temp);
  238.          /* ask for host by name */
  239.          Code = wilAskHostByName((LPSTR)Host);
  240.          if(Code<=0)
  241.            {DisplayError(Code, NULL);
  242.             break;
  243.            }
  244.          /* get host IP address */
  245.          HostAddr = wilGetHostAddr(0);
  246.          if(HostAddr==0)
  247.            {DisplayError(0, "Cannot get IP addess");
  248.             break;
  249.            }
  250.          wsprintf((LPSTR)Temp,"HostAddr = %s\n", wilGetHostDotted(0) );
  251.          DisplayLine((LPSTR)Temp);
  252.          /* create TCP socket */
  253.          Socket = wilTcpSocket();
  254.          if((int)Socket<0)
  255.            {DisplayError((int)Code, NULL);
  256.             break;
  257.            }
  258.          /* attempt to connect to remote host */
  259.          Code = wilConnect(Socket,HostAddr,WHOIS_PORT);
  260.          if(Code<=0)
  261.            {DisplayError(Code, NULL);
  262.             break;
  263.            }
  264.          /* wait up to 10 seconds for a response */
  265.          if(wilIsConnected(Socket,10000))
  266.            {wsprintf((LPSTR)Temp,"Connected: Addr=%lx Port=%d",
  267.                       wilRemoteSockAddr(Socket), wilRemoteSockPort(Socket) );
  268.             DisplayLine((LPSTR)Temp);
  269.            }
  270.          else
  271.            {DisplayLine("Cannot CONNECT");
  272.             break;
  273.            }
  274.          /* send user name */
  275.          wsprintf((LPSTR)Temp,"Whois [%s]",(LPSTR)User);
  276.  
  277. DisplayLine((LPSTR)Temp);
  278.  
  279.          Code = wilWriteString(Socket,(LPSTR)User);
  280.          if(Code<=0)
  281.            {DisplayError(Code, NULL);
  282.             break;
  283.            }
  284.          /* read response */
  285.          while(1)
  286.             {if(!wilDataIsReady(Socket,TEN_SEC)) break;
  287.              Code = wilReadString(Socket,(LPSTR)InBuffer,MAX_BUF);
  288.              if(Code==WIL_EOF) break;
  289.              if(Code>0)
  290.                {wsprintf((LPSTR)Temp,"%s", (LPSTR)InBuffer);
  291.                 DisplayString((LPSTR)Temp);
  292.                }
  293.              DisplayLine("<DONE>");
  294.              wilCloseSocket(Socket);
  295.             }
  296.         }
  297.       else
  298.         {/* add char to input buffer */
  299.          Add2Buffer((char) wParam);
  300.         }
  301.       break;
  302.  
  303.     default:
  304.       return (DefWindowProc(hMainWnd, iMsg, wParam, lParam));
  305.    }
  306.  return 0;
  307.  
  308. } /* end MainWndProc */